/**
 * Created by admins on 2018/1/31.
 */

import Vue from 'vue';
import { Confirm, Alert, Toast, Notify, Loading } from 'vue-ydui/dist/lib.rem/dialog';
import Config from './config.js';
import Tools from './tools.js';

Vue.prototype.$dialog = {
    confirm: Confirm,
    alert: Alert,
    toast: Toast,
    notify: Notify,
    loading: Loading
};
const axiosHttp = {
    getHttp: function (url) {
        Loading.open('');
        var _this = this;
        if (url && url.indexOf("?") < 0) {
            url = url + "?JSSOSESSIONID=" + Config.JSSOSESSIONID;
        } else {
            url = url + "&JSSOSESSIONID=" + Config.JSSOSESSIONID;
        }
        return new Promise(function (resolve, reject) {
            axios.get("http://www.fuerfx.com/cloudfx/" + url).then(function (response) {
                //console.log(response);
                Loading.close();
                if (response.data.success) {
                    //response.data.obj.url = '';
                    response.data = response.data.obj === 0 ? response.data.obj : (response.data.obj ? response.data.obj : response.data);
                    resolve(response.data);
                } else {
                    console.log(response.data);
                    if (response.data.status == 410) {
                        Alert({
                            mes: '暂时没有权限,请联系管理员！',
                            timeout: 2000,
                            icon: 'error',
                            callback: function () {
                                Tools.goBack();
                            }
                        });
                    } else if (response.data.status == 501) {
                        Alert({
                            mes: '登录超时！',
                            timeout: 2000,
                            icon: 'error',
                            callback: function () {
                                Tools.goHome();
                            }
                        });
                    } else {
                        Alert({
                            mes: response.data.msg,
                            timeout: 2000,
                            icon: 'error'
                        });
                    }
                }
            }).catch(function (err) {
                console.log('err');
                //Loading.close();
                //Alert({mes: '服务器繁忙，请稍后再试...'});
            });
        });
    },
    postHttp: function (url, params) {
        Loading.open('');
        var _this = this;
        if (url && url.indexOf("?") < 0) {
            url = url + "?JSSOSESSIONID=" + Config.JSSOSESSIONID;
        } else {
            url = url + "&JSSOSESSIONID=" + Config.JSSOSESSIONID;
        }
        return new Promise(function (resolve, reject) {
            axios.post(url, params).then(function (response) {
                Loading.close();
                if (response.data.success) {
                    response.data = response.data.obj === 0 ? response.data.obj : (response.data.obj ? response.data.obj : response.data);
                    resolve(response.data);
                } else {
                    if (response.data.status == 410) {
                        Toast({
                            mes: '暂时没有权限,请联系管理员！',
                            timeout: 2000,
                            icon: 'error',
                            callback: function () {
                                Tools.goBack();
                            }
                        });
                    } else if (data.status == 501) {
                        Toast({
                            mes: '登录超时！',
                            timeout: 2000,
                            icon: 'error',
                            callback: function () {
                                Tools.goHome();
                            }
                        });
                    } else {
                        Toast({
                            mes: response.data.msg,
                            timeout: 2000,
                            icon: 'error',
                            callback: function () {
                                Tools.goHome();
                            }
                        });
                    }
                }
            }).catch(function (err) {
                Loading.close();
                Alert({mes: '服务器繁忙，请稍后再试...'});
            });
        });
    }
};
export default axiosHttp;